package com.yanfan.data.config;


import com.yanfan.common.constant.YanfanConstant;
import com.yanfan.common.core.domain.entity.SysMenu;
import com.yanfan.common.core.mq.ota.OtaUpgradeDelayTask;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.data.service.IOtaUpgradeService;
import com.yanfan.framework.manager.AsyncManager;
import com.yanfan.system.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.TimerTask;

/**
 * OTA升级监听
 *
 * @author bill
 */
@Slf4j
@Component
@Order(3)
public class UpGradeListener implements ApplicationRunner {

    @Autowired
    private IOtaUpgradeService otaUpgradeService;
    @Resource
    private ISysMenuService menuService;
    /**
     * true: 使用netty搭建的mqttBroker  false: 使用emq
     */
    @Value("${server.broker.enabled}")
    private Boolean enabled;


    @Async(YanfanConstant.TASK.DELAY_UPGRADE_TASK)
    public void listen() {
        while (true) {
            try {
                OtaUpgradeDelayTask task = DelayUpgradeQueue.task();
                if (StringUtils.isNotNull(task)) {
                    Date startTime = task.getStartTime();
                    otaUpgradeService.upgrade(task);
                    log.info("=>开始OTA升级时间{}", startTime);
                }
                continue;
            } catch (Exception e) {
                log.error("=>OTA升级监听异常", e);
            }
        }
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
//        updateMenu();
        AsyncManager.me().execute(new TimerTask() {
            @Override
            public void run() {
        listen();
    }
        });
        log.info("=>OTA监听队列启动成功");
    }

    /**
     * 切换菜单中Emqx控制台和netty管理
     */
    public void updateMenu() {
        SysMenu sysMenu = new SysMenu();
        if (enabled) {
            sysMenu.setMenuId(2104L);
            sysMenu.setVisible("1");
            menuService.updateMenu(sysMenu);
            sysMenu.setMenuId(3031L);
        } else {
            sysMenu.setMenuId(3031L);
            sysMenu.setVisible("1");
            menuService.updateMenu(sysMenu);
            sysMenu.setMenuId(2104L);
        }
        sysMenu.setVisible("0");
        menuService.updateMenu(sysMenu);
    }
}
